import api from "../../../assets/js/api"
import log from "../../../assets/js/log"

export default{
	empsLocalKey:"mes.baogong.emps",
	devicesLocalKey:"mes.baogong.devices",
	prepareCount:0,
	num:0,
	numX:0,
	numH:0,
	numF:0,
	numSum:0,
	numUse:0,
	
	canAddSrcPapers:false,
	
	emp:null,
	emps:[],
	devices:[],
	flow:null,
	plan:null,
	processConfigList:[],
	paperHistoryList:[],
	curProcessConfig:null,
	paperSelected:[],
	
	//设备
	planProcesses:[],
	
	checkTags:[],
	checkGroups:[],
	groupsNames:[],
	clear(){
		this.num = 0
		this.numX = 0
		this.numH = 0
		this.numF = 0
		this.numSum = 0//来料使用量合计
		this.numTotal = 0//来料可用量合计
		this.numUse = 0
		this.flow = null
		this.plan = null
		this.processConfigList = []
		this.paperHistoryList = []
		this.curProcessConfig = null
		this.checkTags = []
		this.checkGroups = []
		this.groupsNames = []
	},
	
	
	autoChooseProcess(){
		let lastProcess = null
		let lastProcessId = 0
		let lastProcessSum = 0
		let curProcessConfig = null
		if (this.paperHistoryList.length === 0){
			if(this.processConfigList.length === 0){
				return false
			}else{
				lastProcess = this.processConfigList[0].process
				lastProcessId = this.processConfigList[0].processId
				curProcessConfig = this.processConfigList[0]
			}
			this.showAddSrcPapers = true
		}else{
			this.paperHistoryList.forEach(paper=>{
				if (paper.num - paper.numUsed > 0 ){
					if (paper.processId !== lastProcessId ){
						lastProcess = paper.process
						lastProcessId = paper.processId
						lastProcessSum = paper.num - paper.numUsed
					}else{
						lastProcessSum += paper.num - paper.numUsed
					}
					paper.numUse =  paper.num - paper.numUsed
				}
			})
			let find = false
			this.processConfigList.forEach(config=>{
				if (find){
					curProcessConfig = config
					find = false
				}
				if (config.processId === lastProcessId){
					find = true
				}
			})
			if (curProcessConfig == null){
				// lastProcess = this.processConfigList[0].process
				// lastProcessId = this.processConfigList[0].processId
				// curProcessConfig = this.processConfigList[0]
			}
		}
		this.lastProcess = lastProcess
		this.lastProcessId = lastProcessId
		this.lastProcessSum = lastProcessSum
		this.curProcessConfig = curProcessConfig
		if (this.curProcessConfig){
			let paperSelected = []
			this.numTotal = 0
			this.numUse = 0
			this.paperHistoryList.map(paper=>{
				if(paper.num - paper.numUsed > 0 && paper.processId != curProcessConfig.processId){
					paperSelected.push(paper)
					this.numTotal+=paper.num - paper.numUsed
					this.numUse+=paper.num - paper.numUsed
				}
			})
			this.paperSelected = paperSelected
			this.paperSelected.forEach(paper=>{
				paper.key = Math.random()
			})
			this.rebuildNums()
			if (this.curProcessConfig.isNeedCheck){
				this.getCheckTags(this.curProcessConfig.checkId)
			}
		}
		return curProcessConfig
	},
	autoFillNumByUse(){
		let numUse = this.numUse
		this.paperSelected.map(paper=>{
			let paperNumAvalid = paper.num - paper.numUsed
			if (numUse > paperNumAvalid){
				paper.numUse = paperNumAvalid
				numUse-=paperNumAvalid
			}else{
				if (numUse > 0){
					paper.numUse = numUse
					numUse = 0
				}else{
					paper.numUse = 0
				}
			}
		})
		this.rebuildNums()
	},
	
	rebuildNums(){
		//从报工记录或者输入框拿到总数
		if (this.paperSelected.length > 0){
			this.numSum = 0
			this.numTotal = 0
			this.numUse = 0
			this.paperSelected.map(paper=>{
				this.numSum +=paper.numUse
				this.numUse +=paper.numUse
				this.numTotal += (paper.num - paper.numUsed)
			})
			//检查是否具有检验数据，来决定修焊废的数据源
			if (this.curProcessConfig.isNeedCheck){
				this.num = this.numSum - this.numX - this.numH - this.numF
			}else{
				this.num = this.numSum - this.numF
			}
		}else{
			this.numSum = this.num + this.numF
		}
	},
	
	getCheckTags(checkProcessId){
		api.post("/mes/admin/CheckTag/list",{
			_where:{processId:checkProcessId},
			_dumps:["group"],
			_order:["groupId","code"],
			limit:0,
		},res=>{
			this.checkTags = res.data.lists
			let groups = []
			let groupsNames = []
			for (let tag of this.checkTags) {
				let index = -1
				for (let i in groups) {
					let group = groups[i]
					if (group.id === tag.groupId){
						index = i
					}
				}
				if (index == -1){
					let group = {...tag.group}
					group.tags = []
					groups.push(group)
					groupsNames.push(group.name)
					index = groups.length - 1
				}
				groups[index].tags.push({...tag,num:null,selected:false})
			}
			this.checkGroups = groups
			this.groupsNames = groupsNames
		})
	},
	getEmpsIdArray(){
		let ids = []
		this.emps.map(emp=>{
			ids.push(emp.id)
		})
		return ids
	},
	getEmpId(){
		if (this.emp == null){
			return 0 
		}
		return this.emp.id*1
	},
	getDeviceId(){
		if(this.devices.length == 1){
			return this.devices[0].id
		}
		return 0
	},
	getFlowId(){
		if (this.flow == null){
			return 0 
		}
		return this.flow.id*1
	},
	getProcessId(){
		if (this.curProcessConfig){
			return this.curProcessConfig.processId
		}
		return 0
	},
	getParams(){
		if (this.curProcessConfig){
			if (this.curProcessConfig.params){
				return this.curProcessConfig.params.fields
			}else{
				return []
			}
		}
		return []
	},
	getSrcPapers(){
		let srcPapers = []
		this.paperSelected.map(paper=>{
			if(paper.numUse > 0 ){
				srcPapers.push({
					srcPaperId:paper.id,
					numUse:paper.numUse*1
				})
			}
		})
		return srcPapers
	},
	deleteSrcPaper(index){
		this.paperSelected.splice(index,1)
		this.rebuildNums()
	},
	getCheckGroupData(){
		let groups = []
		this.checkGroups.map(group=>{
			console.log(group);
			let g = {
				id:group.id,
				tags:[],
			}
			group.tags.map(tag=>{
				if(tag.value*1 > 0){
					g.tags.push({id:tag.id,value:tag.value*1})
				}
			})
			groups.push(g)
		})
		return groups
	},
	getSrcPaperByCode(code){
		api.post("/mes/admin/Paper/list",{
			_where:{
				sn:code,
			},
			_dumps:["process","emp"],
			_order:["-id"],
			page:1,
			limit:0,
		},res=>{
			console.log(res.data.lists);
			let toAdd = res.data.lists.filter(paper=>{
				if (paper.num - paper.numUsed > 0){
					for (let p of this.paperSelected) {
						//这里没有校验是否来自同一个工序
						if (p.id === paper.id){
							return false
						}
					}
					return true
				}
				return false
			})
			this.paperSelected = this.paperSelected.concat(toAdd)
			this.paperSelected.forEach(paper=>{
				paper.key = Math.random()
			})
			this.rebuildNums()
		})
	},
	// addPaperToSelected
}